'use strict';
angular.module('purchase.purchaseOrderSelectSingle', ['app.services'])
	.controller('purchaseOrderSelectSingleCtrl'
			,['$rootScope', '$scope','systemService', 'BizData', '$modalInstance','params', 'title'
			   , f])//控制器结束
    function f($rootScope, $scope, systemService, BizData, $modalInstance, params, title) {


		var Model = this;
		
		Model.gridRowClick = function(event){
			
		}

		Model.gridRowDblClick = function(event){
			Model.ok();
		}
		
		
		var options = {};
		options.httpUrl = $rootScope.getServiceName();
		options.reader = '/purchase/purchaseOrder/single/query';
		options.writer = '';
		options.deleter = '';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.defCols = {fID:{name:"fID",type:"String",label:""},systemCode:{name:"systemCode",type:"String",label:""},billType:{name:"billType",type:"String",label:""},billTypeName:{name:"billTypeName",type:"String",label:""},orderDate:{name:"orderDate",type:"Date",label:""},state:{name:"state",type:"String",label:""},status:{name:"status",type:"String",label:""},audit:{name:"audit",type:"String",label:""},flowType:{name:"flowType",type:"Integer",label:""},confirmDate:{name:"confirmDate",type:"Date",label:""},billLog:{name:"billLog",type:"String",label:""},origin:{name:"origin",type:"String",label:""},originName:{name:"originName",type:"String",label:""},originNo:{name:"originNo",type:"String",label:""},stockNo:{name:"stockNo",type:"String",label:""},factoryID:{name:"factoryID",type:"String",label:""},factoryNo:{name:"factoryNo",type:"String",label:""},factoryName:{name:"factoryName",type:"String",label:""},storageTypeD:{name:"storageTypeD",type:"String",label:""},storageTypeDLabel:{name:"storageTypeDLabel",type:"String",label:""},providerID:{name:"providerID",type:"String",label:""},providerNo:{name:"providerNo",type:"String",label:""},shortName:{name:"shortName",type:"String",label:""},providerName:{name:"providerName",type:"String",label:""},paymentTerm:{name:"paymentTerm",type:"String",label:""},paymentTermLabel:{name:"paymentTermLabel",type:"String",label:""},currency:{name:"currency",type:"String",label:""},currencyLabel:{name:"currencyLabel",type:"String",label:""},exchangeRate:{name:"exchangeRate",type:"Decimal",label:""},printTimes:{name:"printTimes",type:"Integer",label:""},invoiceCategory:{name:"invoiceCategory",type:"String",label:""},invoiceCategoryLabel:{name:"invoiceCategoryLabel",type:"String",label:""},settleCategory:{name:"settleCategory",type:"String",label:""},settleCategoryLabel:{name:"settleCategoryLabel",type:"String",label:""},taxType:{name:"taxType",type:"Integer",label:""},taxTypeLabel:{name:"taxTypeLabel",type:"String",label:""},taxRate:{name:"taxRate",type:"Decimal",label:""},itemAmount:{name:"itemAmount",type:"Decimal",label:""},orderAmount:{name:"orderAmount",type:"Decimal",label:""},orderAmountOld:{name:"orderAmountOld",type:"Decimal",label:""},orderAmountTax:{name:"orderAmountTax",type:"Decimal",label:""},orderAmountTotal:{name:"orderAmountTotal",type:"Decimal",label:""},requirementDate:{name:"requirementDate",type:"Date",label:""},storeItemIng:{name:"storeItemIng",type:"Decimal",label:""},storeItemAmount:{name:"storeItemAmount",type:"Decimal",label:""},orderSize:{name:"orderSize",type:"String",label:""},orderWeight:{name:"orderWeight",type:"Decimal",label:""},orderNo:{name:"orderNo",type:"String",label:""},freightAmount:{name:"freightAmount",type:"Decimal",label:""},depositRate:{name:"depositRate",type:"Decimal",label:""},resultStatus:{name:"resultStatus",type:"String",label:""},paymentStatus:{name:"paymentStatus",type:"String",label:""},paymentStatusLabel:{name:"paymentStatusLabel",type:"String",label:""},contacts:{name:"contacts",type:"String",label:""},mobilePhone:{name:"mobilePhone",type:"String",label:""},fPhone:{name:"fPhone",type:"String",label:""},fAddressID:{name:"fAddressID",type:"String",label:""},fAddressName:{name:"fAddressName",type:"String",label:""},fAddress:{name:"fAddress",type:"String",label:""},fAddressDetail:{name:"fAddressDetail",type:"String",label:""},fProvince:{name:"fProvince",type:"String",label:""},fCity:{name:"fCity",type:"String",label:""},fZone:{name:"fZone",type:"String",label:""},doorBuster:{name:"doorBuster",type:"String",label:""},handling:{name:"handling",type:"String",label:""},handlingLabel:{name:"handlingLabel",type:"String",label:""},detailsCount:{name:"detailsCount",type:"Integer",label:""},detailsCountState:{name:"detailsCountState",type:"Integer",label:""},maxDetailNo:{name:"maxDetailNo",type:"Integer",label:""},employeeNo:{name:"employeeNo",type:"String",label:""},employeeName:{name:"employeeName",type:"String",label:""},attachments:{name:"attachments",type:"String",label:""},comments:{name:"comments",type:"String",label:""},cUsercode:{name:"cUsercode",type:"String",label:""},cUsername:{name:"cUsername",type:"String",label:""},cDatetime:{name:"cDatetime",type:"Datetime",label:""},tUserid:{name:"tUserid",type:"String",label:""},tUsercode:{name:"tUsercode",type:"String",label:""},tUsername:{name:"tUsername",type:"String",label:""},tDatetime:{name:"tDatetime",type:"Datetime",label:""},eUserid:{name:"eUserid",type:"String",label:""},eUsercode:{name:"eUsercode",type:"String",label:""},eUsername:{name:"eUsername",type:"String",label:""},eDatetime:{name:"eDatetime",type:"Datetime",label:""},userId:{name:"userId",type:"String",label:""},enterpriseNo:{name:"enterpriseNo",type:"String",label:""},enterpriseName:{name:"enterpriseName",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = 'fID';
		options.directDeleteMode = true;
		options.limit = 10;
		
		$scope.dataList = new BizData(options);

		//监视对象中的所有变动
//		$scope.dataList.deregRows = $scope.$watch('dataList.rows',$scope.dataList.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
		//////////////////////////////////////////////
		
		//////////////////////对话框使用开始//////////////////////////////////////
		
        //模态窗口打开之后执行的事件
        $modalInstance.opened.then(function(){
    		//接收调用对话框的参数
        	Model.title = title;  
        	Model.params = params;  

    		//设置过滤条件
    		$scope.dataList.filter = "PurchaseOrder.enterpriseNo like '%"+ Model.params.id +"%'"
    		+" AND PurchaseOrder.status='open' "
    		+" AND PurchaseOrder.audit='confirm' "
//    		+" AND (PurchaseOrder.depositAmount-PurchaseOrder.predictAmount-PurchaseOrder.predictAmounting)>0 "
    		;
//    		console.log('Model.params.filter-->',Model.params.filter);
    		if(Model.params.filter!=undefined && Model.params.filter!=''){
    			$scope.dataList.filter = $scope.dataList.filter + " AND " + Model.params.filter;
    		}
//    		console.log('$scope.dataList.filter-->',$scope.dataList.filter);
//    		$scope.dataList.filter = '';

    		//刷新数据
    	    $scope.dataList.refreshData();
        });  
		
        Model.ok = function () {  
	    	//保存成功
	    	//打包数据
	    	var event = {};
	    	var data = [];
	    	var sourceRow = $scope.dataList.rows[$scope.dataList.currentIndex];
	    	var row = {};
			//循环行中的列，判断变化点是哪一个
			for(var obj in sourceRow){
				if(obj!='$$hashKey' && obj!='userdata'){
					row[obj] = sourceRow[obj].value;
				}
			}
	    	
	    	data.push(row);
	    	
	    	event.data = data;
			$modalInstance.close(event);  
	    };  
	    
	    Model.cancel = function () {  
	        $modalInstance.dismiss('cancel');  
	    };    


	  //////////////////////对话框使用结束//////////////////////////////////////
	    



	    return Model;
	    
	}	
	
